package com.business.business.web.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.business.common.base.BaseService;
import com.business.business.model.BusinessBasisinfoFiles;
import com.business.business.model.BusinessFiretraining;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.BusinessBasisinfoFilesMapper;
import com.business.business.web.mapper.BusinessFiretrainingMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class FiretrainingService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(FiretrainingService.class);
    @Resource
    private BusinessFiretrainingMapper businessFiretrainingMapper;
    @Resource
    private BusinessBasisinfoFilesMapper businessBasisinfoFilesMapper;
    public RespEntity firetrainingAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String businessId = "";
        if ("1".equals(userType)){
            businessId = jsonParam.getStr("businessId");
        }else {
            businessId = home_id;
        }
        //获取前台的数据
        String participant = jsonParam.getStr("participant");
        String trainingcontents = jsonParam.getStr("trainingcontents");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        //自动生成ID
        String uuid = UUID.randomUUID().toString();
        //自动生成maketime
        Date date=new Date();
//        String makingTime = DateUtil.now();
        String createyear = DateUtil.year(date)+"";

        BusinessFiretraining businessFiretraining = new BusinessFiretraining();
        businessFiretraining.setId(uuid);
        businessFiretraining.setTrainingtime(jsonParam.getDate("trainingtime"));
        businessFiretraining.setParticipant(participant);
        businessFiretraining.setTrainingcontents(trainingcontents);
        businessFiretraining.setBusinessId(businessId);
        businessFiretraining.setCreatetime(date);
        businessFiretraining.setCreateyear(createyear);

        boolean businessFiretrainingInsert = businessFiretrainingMapper.insert(businessFiretraining)>0?true:false;
            BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
            businessBasisinfoFiles.setBusinessBasisinfoId(uuid);
            businessBasisinfoFiles.setFileName(fileName);
            businessBasisinfoFiles.setFilePath(filePath);
        boolean insertBusinessBasisinfoFiles  = businessBasisinfoFilesMapper.insert(businessBasisinfoFiles)>0?true:false;

        if (insertBusinessBasisinfoFiles && insertBusinessBasisinfoFiles) {
            return RespEntity.SUCCESS;
        } else {
            return RespEntity.fail("插入失败");
        }
    }

    /*
     * @Author LiHaoDong
     * @Description 消防培训列表
     * @Date 15:05 2020/7/22
     * @Param
     * @return
     **/
    public RespEntity firetrainingListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        if ("2".equals(userType)){
            jsonParam.put("businessId",home_id);
        }
        Page<BusinessFiretraining> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        IPage<BusinessFiretraining> iPage = this.businessFiretrainingMapper.rulesandregulationsListPage(page, jsonParam);
        List<BusinessFiretraining> records = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalSize", iPage.getTotal());
        data.put("pages", iPage.getPages());
        data.put("pageSize", jsonParam.getInt("limit"));
        data.put("Items",records);
        data.put("currentPage",jsonParam.getInt("page"));
        data.put("currentSize",jsonParam.getInt("limit"));
        return RespEntity.success(data);

    }

    /**
     * @Author LiHaoDong
     * @Description 消防培训详情
     * @Date 15:35 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firetrainingDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        BusinessFiretraining businessFiretraining = this.businessFiretrainingMapper.firetrainingDetail(jsonParam);
        if (!businessFiretraining.isEmpty()) {
            return RespEntity.success(businessFiretraining);
        }
        return RespEntity.fail("查询失败");
    }

    /**
     * @Author LiHaoDong
     * @Description 消防培训修改
     * @Date 15:45 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firetrainingUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String id = jsonParam.getStr("id");
        String trainingtime = jsonParam.getStr("trainingtime");
        DateTime parse = null;
        if (trainingtime != null) {
            parse = DateUtil.parse(trainingtime);
        }
        String participant = jsonParam.getStr("participant");
        String trainingcontents = jsonParam.getStr("trainingcontents");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        //删除
        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);
        int delete = this.businessBasisinfoFilesMapper.delete(queryWrapper);
        //插入
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setBusinessBasisinfoId(id);
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        int insert = this.businessBasisinfoFilesMapper.insert(businessBasisinfoFiles);

        //修改
        BusinessFiretraining businessFiretraining = new BusinessFiretraining();
        businessFiretraining.setTrainingtime(parse);
        businessFiretraining.setParticipant(participant);
        businessFiretraining.setTrainingcontents(trainingcontents);
        businessFiretraining.setId(id);
        int update = this.businessFiretrainingMapper.updateById(businessFiretraining);
        if (delete > 0 && insert > 0 && update > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("修改失败");
    }

    /**
     * @Author LiHaoDong
     * @Description 消防培训删除
     * @Date 16:04 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firetrainingDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String id = jsonParam.getStr("id");

        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);
        int deleteBasisinfoFiles = this.businessBasisinfoFilesMapper.delete(queryWrapper);

        int deleteFiretraining = this.businessFiretrainingMapper.deleteById(id);

        if (deleteBasisinfoFiles > 0 && deleteFiretraining > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("删除失败");
    }
}
